package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.LinkBean;
import com.project.dao.ILinkDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.time.LocalDate;

/**
 * 通讯录持久接口的实现类
 */
@Repository
public class LinkDaoImpl extends BaseDao implements ILinkDao {
    @Override
    public CutPageBean<LinkBean> cutAll(int pageNo) {
        CutPageBean<LinkBean> cut = new CutPageBean();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_link limit ?,?");
            ps.setObject(1,(pageNo-1)*CutPageBean.PAGESIZE);
            ps.setObject(2,CutPageBean.PAGESIZE);
            res= ps.executeQuery();
            while (res.next()){
                LinkBean link  = new LinkBean();
                link.setBirthday(LocalDate.parse(res.getString("l_birthday")));
                link.setName(res.getString("l_name"));
                link.setId(res.getInt("pk_linkId"));
                link.setPhone(res.getString("l_phone"));
                cut.getList().add(link);
            }
            ps = con.prepareStatement("select count(*) c from t_link");
            res=ps.executeQuery();
            while (res.next()){
                cut.setCount(res.getInt("c"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return cut;
    }

    @Override
    public void add(LinkBean link) {
        this.setConnection();
        try {
            ps = con.prepareStatement("insert into t_link(l_name,l_birthday,l_phone)"+"values(?,?,?)");
            ps.setObject(1,link.getName());
            ps.setObject(2,link.getBirthday());
            ps.setObject(3,link.getPhone());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void del(Integer id) {
        this.setConnection();
        try {
            ps=con.prepareStatement("delete from t_link where pk_linkId=?");
            ps.setObject(1,id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

    }

    @Override
    public void update(Integer id, String phone) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_link set l_phone=? where pk_linkId=?");
            ps.setObject(1,phone);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public LinkBean byId(Integer id) {
        LinkBean link = new LinkBean();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_link where pk_linkId=?");
            ps.setObject(1,id);
            res = ps.executeQuery();
            while (res.next()){
                link.setBirthday(LocalDate.parse(res.getString("l_birthday")));
                link.setName(res.getString("l_name"));
                link.setId(res.getInt("pk_linkId"));
                link.setPhone(res.getString("l_phone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return link;
    }

}
